import { login, /* logout */ getInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import commonApi from '@/api/commonApi'

const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    menus: [],
    permissions: [],
    hasPermission: true,
    isAdmin: false,
    user: {} // 用户基础信息
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_MENUS: (state, menus) => {
      state.menus = menus
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
    },
    SER_HASPERMISSION: (state, hasPermission) => {
      state.hasPermission = hasPermission
    },
    SER_IS_ADMIN: (state, isAdmin) => {
      state.isAdmin = isAdmin
    },
    SET_USER: (state, user) => {
      state.user = user
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username && userInfo.username.trim()
      return new Promise((resolve, reject) => {
        login(username, userInfo.password, userInfo.MYSAPSSO2).then(response => {
          const data = response.data
          setToken(data)
          commit('SET_TOKEN', data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        commonApi('/user/details', 'get').then(response => {
          const roles = response.data.role
          const isAdmin = roles.some(role => role.admin)
          const user = { id: response.data.id,
            real_name: response.data.realName,
            username: response.data.loginName }
          commit('SER_IS_ADMIN', isAdmin)
          commit('SET_USER', user)
        })
        getInfo().then(response => {
          const data = response.data
          if (data && data.length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_MENUS', data)
          } else {
            reject('getInfo: menus must be a non-null array !')
          }
          /* commit('SET_NAME', data.name)
          commit('SET_AVATAR', data.avatar)*/
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    LogOut({ commit, state }) {
      commit('SET_TOKEN', '')
      commit('SET_ROLES', [])
      sessionStorage.clear()
      removeToken()
      /* return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })*/
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        commit('SET_ROLES', [])
        removeToken()
        resolve()
      })
    }
  }
}

export default user
